package com.chao.deeply.lock;

import java.util.concurrent.locks.ReentrantLock;

/**
 * @author : Rookie
 * @description : 公平锁 非公平锁
 * @date : 2023/8/7
 */
public class UnFairReentrantLockDemo {

    public static void main(String[] args) {
        LockTask lockTask = new LockTask();
        new Thread(lockTask,"t1").start();
        new Thread(lockTask,"t2").start();
        new Thread(lockTask,"t3").start();
        new Thread(lockTask,"t4").start();
        new Thread(lockTask,"t5").start();
        new Thread(lockTask,"t6").start();
    }

    static class LockTask implements Runnable {
        // 非公平锁
//        private final ReentrantLock lock = new ReentrantLock();
        // 公平锁
        private final ReentrantLock lock = new ReentrantLock(true);
        @Override
        public void run() {
            try {
                lock.lock();
                System.out.println("线程" + Thread.currentThread().getName() + "获取到锁！");
            } finally {
                lock.unlock();
            }
        }
    }
}
